package com.greenline.push.home.controllers.domain;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.DomainAppChannelManager;
import com.greenline.push.dal.pushcenter.dataobject.DomainAppChannelDO;
import com.greenline.push.dal.query.DomainAppChannelQuery;

@Controller
@RequestMapping(value = "/domainappchannel")
public class DomainAppChannelController {

    @Autowired
    private DomainAppChannelManager domainAppChannelManager;
    private final Log logger = LogFactory.getLog(DomainAppChannelController.class);

    @GuserResource("应用模板域关系配置列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "domainappchannel/index";
    }

    @GuserResource("域渠道勾选页面")
    @RequestMapping(value = "/listall-domainchannel/{domainid}", method = RequestMethod.GET)
    public String listalldomainchannel(ModelMap model, @PathVariable Long domainid) {
        model.put("domainId", domainid);
        return "domainappchannel/listall-domainchannel";
    }

    @GuserResource("应用模板域关系域配置列表")
    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<DomainAppChannelDO> listRecord(@ModelAttribute("query") DomainAppChannelQuery query, String domainId,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<DomainAppChannelDO> ret = new GridJsonData<DomainAppChannelDO>();
        ret.setQuery(domainAppChannelManager.listByQuery(query));
        return ret;
    }

    @GuserResource("域渠道关系配置")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addInit(ModelMap model) {
        return "domainappchannel/add";
    }

    @GuserResource("域渠道关系域配置添加")
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson add(ModelMap model, @ModelAttribute("domainAppchannel") DomainAppChannelDO domainAppchannel) {
        BaseJson json = new BaseJson();
        try {
            DomainAppChannelQuery query = new DomainAppChannelQuery();
            query.setDomainId(domainAppchannel.getDomainId());
            query.setAppChannelId(domainAppchannel.getAppChannelId());
            domainAppChannelManager.listByQuery(query);
            List<DomainAppChannelDO> sResult = query.getDataList();
            boolean insertResult = false;
            if (sResult == null || sResult.size() == 0) {
                insertResult = domainAppChannelManager.insert(domainAppchannel) > 0;
            }
            if (insertResult) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错,请检查是否重复绑定");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错" + e.getMessage());
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("域渠道配置修改")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateInit(ModelMap model, @PathVariable Long id) {
        DomainAppChannelDO object = domainAppChannelManager.getById(id);
        model.put("domainAppchannel", object);
        return "domainappchannel/update";
    }

    @GuserResource("域渠道修改")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson update(ModelMap model, @ModelAttribute("domainAppchannel") DomainAppChannelDO domainAppchannel) {
        BaseJson json = new BaseJson();
        try {
            if (domainAppChannelManager.update(domainAppchannel)) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("域channel配置删除")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson delete(ModelMap model, @ModelAttribute("id") Long id) {
        BaseJson json = new BaseJson();
        try {
            if (domainAppChannelManager.delete(id)) {
                json.setMessage("删除成功！");
            } else {
                json.setHasError(true);
                json.setMessage("删除出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    // @GuserResource("模板域列表")
    // @RequestMapping(value = "/list-alldomain")
    // public String listAllDomain(ModelMap model) {
    // return "appdomaintemplate/listall-domain";
    // }
    //
    // @GuserResource("应用模板列表")
    // @RequestMapping(value = "/list-allapptemplate")
    // public String listAllAppTemplate(ModelMap model) {
    // return "appdomaintemplate/listall-apptemplate";
    // }
    //
    // @GuserResource("应用模板域关系域配置修改")
    // @RequestMapping(value = "/listbytemplate/{id}", method =
    // RequestMethod.GET)
    // public String getByDomainId(@PathVariable Long id, ModelMap model
    // // ,RedirectAttributes attr, HttpServletResponse response
    // ) throws IOException {
    // if (id == null) {
    // id = 0L;
    // }
    // model.put("id", id);
    // // attr.addFlashAttribute("id", 3);
    // // response.sendRedirect("?id="+3);
    // return "appdomaintemplate/listall-apptemplate";
    // }
}
